﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TpBaseObjects.Interfaces;
using TpBaseObjects.Objects;
using TpContracts;
using TpData.Manager;
using TpNotiz.Forms;
using TpNotiz.Interfaces;
using TpTools;

namespace TpNotiz.Controller {

    public class MainController : IController {

        #region Members

        private TecNotizMain myForm;
        private const int interval = 120000;

        #endregion

        #region Constructor

        public MainController(ITecForm form) {
            myForm = form as TecNotizMain;
        }

        #endregion

        #region Methods

        public void Close(object sender, EventArgs e) {
            TecUserDataManager.SaveUserLogout(Contracts.MyUser.UserID);
        }

        public void CreateNewNotiz(object sender, EventArgs e) {
            DateTime now = DateTime.Now;
            TecNotizObj newNotiz = new TecNotizObj(now, myForm.CurrentUser.UserID);
            newNotiz.NotizID = 0;
            newNotiz.AnlageDatum = now;
            newNotiz.TecUser = myForm.CurrentUser.LoginName;
            newNotiz.AnrufType = TpExtensions.Extensions.TelType.Interessent;
            newNotiz.Status = 1;

            Organisation org = new Organisation();
            org.OrgName = "";

            TecNotiz tn = new TecNotiz(newNotiz);
            tn.MyOrgModel = org;
            tn.ShowDialog();
        }

        public void Dispose() {
        }

        public void LoadModels() {
            myForm.CurrentUser = TecUserDataManager.LoadTecUser(Environment.UserName);
            myForm.MyModel = TecNotizDataManager.LoadTecNotizList(myForm.CurrentUser.UserID);
            myForm.MyUserModel = TecUserDataManager.LoadTecUser();
            
        }

        public void OpenDetailForm(object sender, DataGridViewCellEventArgs e) {
            DataGridView dgv = sender as DataGridView;
            DataGridViewRow row = dgv.CurrentRow;

            TecNotizObj tnModel = row.DataBoundItem as TecNotizObj;
            TecNotizObj tnClone = (TecNotizObj)tnModel.Clone();

            TecNotiz tn = new TecNotiz(tnClone);
            tn.ShowDialog();

            SelectedIndexChanged(sender, e);
            myForm.SetFormDataSources();
        }

        private void ReloadData(int userID) {
            myForm.MyModel = TecNotizDataManager.LoadTecNotizList(userID);
            //ataGridView dgv = myForm.Controls["
        }

        public void SelectedIndexChanged(object sender, EventArgs e) {
            myForm.myTimer.Stop();
            ComboBox cmb = myForm.Controls["comboBox1"] as ComboBox;
            if (cmb.SelectedValue != null) {
                ReloadData((int)cmb.SelectedValue);
            }
            myForm.myTimer.Enabled = true;
            myForm.SetFormDataSources();
        }

        public void SetTimer() {
            myForm.myTimer = new System.Windows.Forms.Timer();
            myForm.myTimer.Tick += new EventHandler(TimerEventProcessor);
            myForm.myTimer.Interval = interval;
            myForm.myTimer.Start();
        }

        public void Save(IBaseObject myObject) {
            throw new NotImplementedException("MainController.Save");
        }

        private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
            myForm.myTimer.Stop();
            ComboBox cmb = myForm.Controls["combobox1"] as ComboBox;
            ReloadData((int)cmb.SelectedValue);
            myForm.SetFormDataSources();
            myForm.myTimer.Enabled = true;
        }

        #endregion

    }
}
